package com.hk.housekeeping.service.impl;

import com.hk.housekeeping.entity.Staff;
import com.hk.housekeeping.mapper.StaffMapper;
import com.hk.housekeeping.service.StaffServ;
import com.hk.housekeeping.utils.PageUtils;
import com.hk.housekeeping.utils.ResponseCode;
import com.hk.housekeeping.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class StaffServImpl implements StaffServ {
    @Autowired
    private StaffMapper staffMapper;

    @Override
    public ResponseEntity<Integer> addStaff(Staff staff) {
        ResponseEntity<Integer> entity = null;
        int count = staffMapper.addStaff(staff);
        if (count != 0) {
            entity = ResponseEntity.success(count);
        } else {
            entity = ResponseEntity.error(ResponseCode.STAFF_ADD_ERROR);
        }
        return entity;
    }

    @Override
    public ResponseEntity<Integer> delStaff(int id) {
        ResponseEntity<Integer> entity = null;
        int count = staffMapper.delStaff(id);
        if (count != 0) {
            entity = ResponseEntity.success(count);
        } else {
            entity = ResponseEntity.error(ResponseCode.STAFF_ADD_ERROR);
        }
        return entity;
    }

    @Override
    @Deprecated
    public ResponseEntity<Staff> selectStaff(int id) {
        Staff staff = staffMapper.returnStaffInfo(id);
        ResponseEntity<Staff> entity = null;
        if (!StringUtils.isEmpty(staff)) {
            entity = ResponseEntity.success(staff);
        } else {
            entity = ResponseEntity.error(ResponseCode.STAFF_SELECT_ERROR);
        }
        return entity;
    }

    @Override
    public ResponseEntity<Staff> selectStaffHonor(int id) {
        Staff staff = staffMapper.returnStaffInfoHonor(id);
        ResponseEntity<Staff> entity = null;
        if (!StringUtils.isEmpty(staff)) {
            entity = ResponseEntity.success(staff);
        } else {
            entity = ResponseEntity.error(ResponseCode.STAFF_SELECT_ERROR);
        }
        return entity;
    }

    @Override
    public ResponseEntity<Integer> editStaff(Staff staff) {
        ResponseEntity<Integer> entity = null;
        if (!StringUtils.isEmpty(staff)) {
            //先进行删除
            int count1 = staffMapper.delStaff(staff.getId());
            //再插入
            int count2 = staffMapper.addStaff(staff);
            if (count1 == 0) {
                System.out.println(ResponseCode.STAFF_DEL_ERROR.getMsg());
                entity = ResponseEntity.error(ResponseCode.STAFF_EDIT_ERROR);
            } else if (count2 == 0) {
                System.out.println(ResponseCode.STAFF_ADD_ERROR.getMsg());
                entity = ResponseEntity.error(ResponseCode.STAFF_EDIT_ERROR);
            } else {
                entity = ResponseEntity.success(1);
            }
        } else {
            System.out.println("Staff对象为空！");
            entity = ResponseEntity.error(ResponseCode.STAFF_EDIT_ERROR);
        }

        return entity;
    }

    @Override
    public ResponseEntity<PageUtils<List<Staff>>> pageSelect(int currentPage, int pageSize) {
        List<Staff> staffs = staffMapper.pageSelect((currentPage - 1) * pageSize, pageSize);
        PageUtils<List<Staff>> pageList = new PageUtils<>();
        //改变当前页（设置为请求参数的页数）
        pageList.setCurrentPage(currentPage);
        //设置返回总页数
        pageList.setTotalCount(getTotalCount().getData());
        //设置返回内容
        pageList.setData(staffs);

        ResponseEntity<PageUtils<List<Staff>>> entity = ResponseEntity.success(pageList);
        return entity;
    }

    @Override
    public ResponseEntity<Integer> getTotalCount() {
        int totalCount = staffMapper.getTotalCount();
        ResponseEntity<Integer> entity = ResponseEntity.success(totalCount);
        return entity;
    }

    @Override
    public ResponseEntity<PageUtils<List<Staff>>> selectStaffByName(String name, int currentPage, int pageSize) {
        ResponseEntity<PageUtils<List<Staff>>> entity = null;

        int totalCountByName = getSelectByNameCount(name);
        if (totalCountByName != 0) {
            PageUtils<List<Staff>> pageUtils = new PageUtils<>();
            List<Staff> staffs = staffMapper.getStaffByName(name, (currentPage - 1) * pageSize, pageSize);
            pageUtils.setData(staffs);
            pageUtils.setCurrentPage(currentPage);
            pageUtils.setTotalCount(totalCountByName);

            entity = ResponseEntity.success(pageUtils);
        } else {
            entity = ResponseEntity.error(ResponseCode.STAFF_SEARCH_BY_NAME_ERROR);
        }

        return entity;
    }

    @Override
    public ResponseEntity<PageUtils<List<Staff>>> selectStaffByPhone(String phone) {
        ResponseEntity<PageUtils<List<Staff>>> entity = null;

        Staff staff = staffMapper.getStaffByPhone(phone);
        if (!StringUtils.isEmpty(staff)) {
            List<Staff> staffList = new ArrayList<>();
            staffList.add(staff);
            PageUtils<List<Staff>> pageUtils = new PageUtils<>();
            pageUtils.setData(staffList);
            pageUtils.setTotalCount(1);

            entity = ResponseEntity.success(pageUtils);
        } else {
            entity = ResponseEntity.error(ResponseCode.STAFF_SEARCH_BY_PHONE_ERROR);
        }
        return entity;
    }

    @Override
    public int getSelectByNameCount(String name) {
        int totalCountByName = staffMapper.getSelectByNameCount(name);
        return totalCountByName;
    }


}
